package com.cans.lightning.business.lowcode.service.impl;

import com.alibaba.fastjson.JSON;
import com.cans.lightning.base.mapper.IBaseMapper;
import com.cans.lightning.base.service.impl.BaseServiceImpl;
import com.cans.lightning.business.lowcode.dao.AppFormViewDao;
import com.cans.lightning.business.lowcode.dao.AppFormViewItemDao;
import com.cans.lightning.business.lowcode.dto.AppFormViewDto;
import com.cans.lightning.business.lowcode.entity.AppFormView;
import com.cans.lightning.business.lowcode.entity.AppFormViewItem;
import com.cans.lightning.business.lowcode.manager.form.bean.ViewBean;
import com.cans.lightning.business.lowcode.manager.form.bean.ViewItemBean;
import com.cans.lightning.business.lowcode.mapper.AppFormViewMapper;
import com.cans.lightning.business.lowcode.service.api.IAppFormViewService;
import org.beetl.sql.mapper.BaseMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 表单视图
 *
 * @author shencan
 * @date 2020/6/14 20:53
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AppFormViewServiceImpl extends BaseServiceImpl<AppFormView,  AppFormViewDto, String> implements IAppFormViewService {

    private static final Logger logger = LoggerFactory.getLogger(AppFormViewServiceImpl.class);

    @Resource
    private AppFormViewDao appFormViewDao;
    @Resource
    private AppFormViewItemDao appFormViewItemDao;
    @Resource
    private AppFormViewMapper appFormViewMapper;

    @Override
    public BaseMapper<AppFormView> getDaoImpl() {
        return appFormViewDao;
    }

    @Override
    public IBaseMapper<AppFormView, AppFormViewDto, String> getMapperImpl() {
        return appFormViewMapper;
    }

    @Override
    public List<AppFormViewDto> findByFormId(String formId) {
        List<AppFormView> select = appFormViewDao.createLambdaQuery().andEq(AppFormView::getFormId, formId).asc(AppFormView::getSortId).select();
        return appFormViewMapper.toDtos(select);
    }

    @Override
    public void saveOrUpdate(ViewBean viewBean) {
        AppFormView appFormView = new AppFormView();
        appFormView.setId(viewBean.getId());
        appFormView.setFormId(viewBean.getFormId());
        appFormView.setViewName(viewBean.getViewName());
        appFormView.setFieldBind(JSON.toJSONString(viewBean.getFieldBind()));
        this.saveOrUpdate(appFormView);
        long sortId = 0;
        List<ViewItemBean> viewItemList = viewBean.getViewItemList();
        for (ViewItemBean viewItemBean : viewItemList) {
            AppFormViewItem appFormViewItem = new AppFormViewItem();
            appFormViewItem.setViewId(appFormView.getId());
            appFormViewItem.setId(viewItemBean.getId());
            appFormViewItem.setShowType(viewItemBean.getShowType());
            appFormViewItem.setFormId(viewBean.getFormId());
            appFormViewItem.setRefObj(JSON.toJSONString(viewItemBean.getRefObj()));
            appFormViewItem.setSortId(sortId ++);
            this.saveOrUpdate(appFormViewItem);
        }
    }

    @Override
    public void saveOrUpdate(AppFormViewItem appFormViewItem){
        if(appFormViewItem.getId() == null){
            appFormViewItemDao.insert(appFormViewItem);
        }else{
            appFormViewItemDao.updateById(appFormViewItem);
        }
    }

    @Override
    public List<AppFormViewItem> findItemByFormId(String formId){
        return appFormViewItemDao.createLambdaQuery().andEq(AppFormViewItem::getFormId,formId).asc(AppFormViewItem::getSortId).select();
    }
}
